//
//  INHtmlElement.m
//  chanlin
//
//  Created by Ryan on 14-11-13.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "INHtmlElement.h"
#import "AttributeStringEntity.h"
#import <CoreText/CoreText.h>
#import "INHtmlElementUtil.h"

@implementation INHtmlElement

- (instancetype)init{
    self=[super init];
    if(self){
        self.strValue = [NSMutableString string];
    }
    return self;
}

- (NSMutableAttributedString *)attributedStringWithContent{
    if(_isStrNULL(self.strValue)) return nil;
     NSMutableAttributedString *textAttributeTemp = [[NSMutableAttributedString alloc] initWithString:[self.strValue stringByAppendingString:@"\n"]];
    //__weak typeof(self) me=self;
    [self.attributeEntitys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        AttributeStringEntity *attributeEntity = (AttributeStringEntity *)obj;
        // 上色
        CGColorRef cgColor = NULL;
        cgColor=attributeEntity.textColor.CGColor;
        UIFont *currrentFont=nil;
        if(attributeEntity.boldText){
            currrentFont=[INHtmlElementUtil sharedInstance].textBlod;
        }else{
            currrentFont=[INHtmlElementUtil sharedInstance].textFont;
            
        }
        
     //    字体、大小
        CTFontRef ctFont =
        CTFontCreateWithName((__bridge CFStringRef)currrentFont.fontName, currrentFont.pointSize, NULL);
        [textAttributeTemp addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)cgColor range:attributeEntity.textRange];
        [textAttributeTemp addAttribute:(id)kCTFontAttributeName value:(__bridge id)ctFont range:attributeEntity.textRange];
        CFRelease(ctFont);
//        [textAttributeTemp addAttribute:NSForegroundColorAttributeName value:attributeEntity.textColor range:attributeEntity.textRange];
//        [textAttributeTemp addAttribute:NSFontAttributeName value:attributeEntity.textFont range:attributeEntity.textRange];
        
    }];
    
    return textAttributeTemp;
    
}

@end
